package com.holi.hiteacher.fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.holi.hiteacher.R;
import com.holi.hiteacher.bean.StudyQuestInfo;
import com.joshua.xlistview.XListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

import static com.holi.hiteacher.utils.DataUtils.date2str_type3;

/**
 * @description:
 * @author: JoshuaWong E-mail:joshuawongchn@gmail.com
 * @date: 2016年12月23日 17:50
 * @version: 1.0
 */
public class StudyCircleFragment extends Fragment implements XListView.IXListViewListener {
    @BindView(R.id.id_study_circle_list)
    XListView xListView;

    ArrayList<StudyQuestInfo> mDatas = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_study_circle, container, false);
        ButterKnife.bind(this, view);
        setHasOptionsMenu(true);

        initData();
        xListView.setPullRefreshEnable(true);
        xListView.setPullLoadEnable(true);
        xListView.setAdapter(new MyAdapter(getActivity(), R.layout.item_study_circle_info,
                mDatas));
        xListView.setXListViewListener(this);

        return view;
    }

    private void initData() {
        mDatas.add(new StudyQuestInfo());
        mDatas.add(new StudyQuestInfo());
        mDatas.add(new StudyQuestInfo());
        mDatas.add(new StudyQuestInfo());
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        menu.clear();
        inflater.inflate(R.menu.study_circle_menu, menu);
    }

    @Override
    public void onRefresh() {
        Log.w("joshua", "onRefresh: ");
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                xListView.stopRefresh();
            }
        }, 1000);
    }

    @Override
    public void onLoadMore() {
        Log.w("joshua", "onLoadMore: ");
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                xListView.stopLoadMore();
            }
        }, 1000);
    }


    class MyAdapter extends ArrayAdapter<StudyQuestInfo> {
        private LayoutInflater mInflater;

        public MyAdapter(Context context, int resource, List<StudyQuestInfo> objects) {
            super(context, resource, objects);
            mInflater = LayoutInflater.from(context);
        }

        @NonNull
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item_study_circle_info, parent,
                        false);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.setData(mDatas.get(position));

            return convertView;
        }

        class ViewHolder {
            @BindView(R.id.id_study_portrait)
            CircleImageView imgHead;
            @BindView(R.id.id_study_name)
            TextView tvName;
            @BindView(R.id.id_study_date_time)
            TextView tvDate;
            @BindView(R.id.id_study_content)
            TextView tvContent;

            public ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }

            public void setData(StudyQuestInfo info) {
                imgHead.setImageResource(info.portrait);
                tvName.setText(info.name);
                tvDate.setText(date2str_type3(info.date));
                tvContent.setText(info.content);
            }

        }
    }

}
